﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

namespace Bussiness
{
    /// <summary>
    /// 启停越限类
    /// </summary>
    public class StartStop
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int ID_KEY { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string PARANAME { get; set; }
        /// <summary>
        /// 生成启停信息列
        /// </summary>
        /// <returns></returns>
        private DataTable GenerateTableColumn()
        {
            DataTable newDT = new DataTable();

            DataColumn col1 = new DataColumn("ID_KEY");
            DataColumn col2 = new DataColumn("PARANAME");
            DataColumn col3 = new DataColumn("BEGINTIME");
            DataColumn col4 = new DataColumn("ENDTIME");
            DataColumn col5 = new DataColumn("STARTTIME");

            newDT.Columns.Add(col1);
            newDT.Columns.Add(col2);
            newDT.Columns.Add(col3);
            newDT.Columns.Add(col4);
            newDT.Columns.Add(col5);

            return newDT;
        }

        /// <summary>
        /// 返回时间比较查询结果
        /// </summary>
        /// <param name="startDate">起始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="paraID">统计量编号</param>
        /// <returns></returns>
        public DataTable CompareData(string startDate, string endDate, string paraID)
        {
            string sql = "";
            if (startDate == "")
            {
                throw new Exception("起始时间不能为空！");
            }

            if (endDate == "")
                sql = "SELECT * FROM ADMINISTRATOR.EVENTREPORT WHERE DATE(EVENTBEGIN)>='" + startDate + "' AND PARAID ='" + paraID + "'";
            else
                sql = "SELECT * FROM ADMINISTRATOR.EVENTREPORT WHERE DATE(EVENTBEGIN)>='" + startDate + "' AND DATE(EVENTBEGIN)<='" + endDate + "' AND PARAID ='" + paraID + "'";

            DateTime sDate = new DateTime();
            DateTime eDate = new DateTime();
            SysParament para = new SysParament();
            DataTable newDT = GenerateTableColumn();

            DB2Provider.DataLink link = new DB2Provider.DataLink();
            DataTable dt = link.ExcuteRetureTable(sql);
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow newRow = newDT.NewRow();
                    newRow[0] = dr[0];

                    string paraDesc = para.GetParaDescription(paraID);
                    newRow[1] = paraDesc;

                    sDate = (DateTime)dr[3];
                    newRow[2] = sDate.ToString();
                    if (dr[4] != null && dr[4] != System.DBNull.Value)
                    {
                        TimeSpan ts = new TimeSpan();
                        eDate = (DateTime)dr[4];
                        newRow[3] = eDate.ToString();

                        ts = eDate.Subtract(sDate);
                        DB2Time dbTime = new DB2Time();

                        newRow[4] = dbTime.FormatDiffTime(ts);
                    }
                    newDT.Rows.Add(newRow);
                }
            }

            return newDT;
        }
        /// <summary>
        /// 通过ID_KEY删除启停事件数据
        /// </summary>
        /// <param name="id_key">编号</param>
        public void Delete(int id_key)
        {
            string sql = "DELETE FROM ADMINISTRATOR.EVENTREPORT WHERE ID_KEY=" + id_key;
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }
        /// <summary>
        /// 修改启停事件数据
        /// </summary>
        /// <param name="sDate">起始时间</param>
        /// <param name="eDate">结束时间</param>
        public void Update(DateTime sDate, DateTime eDate, int ID_KEY)
        {
            string sql = "UPDATE ADMINISTRATOR.EVENTREPORT SET EVENTBEGIN='" + sDate.ToString() + "' , EVENTEND='" + eDate.ToString() + "' WHERE ID_KEY=" + ID_KEY;

            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }
    }
}
